/*
 * CS_REQ_OPEN_SLOT.cpp
 *
 *  Created on: 2013. 10. 30.
 *      Author: mega@gamevil.com
 */

#include "../../Command.h"
#include "../../User.h"
#include "../../NResource/Static.h"
#include "../../NLogic/VirtualCurrency.h"

namespace ProjectZ { namespace NCommand {

void Command::CS_REQ_OPEN_SLOT(User * pUser, ::Network::Packet * pPacket)
{
	PREPARE_PACKET;

	char slot_number = 0;
	*pPacket >> slot_number;

	if (2 > slot_number || 7 < slot_number)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->Write2(ACK_UNKNOWN_ERROR);
		pAck->Write1(slot_number);
		pAck->MakePacket(CMD_SC_REQ_OPEN_SLOT);
		pUser->Send(pAck);
		return;
	}

	if (true == pUser->GetUserInfo().array_Slot[static_cast<int>(slot_number)].open)
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->Write2(ACK_UNKNOWN_ERROR);
		pAck->Write1(slot_number);
		pAck->MakePacket(CMD_SC_REQ_OPEN_SLOT);
		pUser->Send(pAck);
		return;
	}

	NResource::VCResource::Flyweight * pFly =
			NResource::Static::InstancePtr()->GetVCRef()->GetFly(EM_VCINDEX_SLOT_OPEN);
	if (NULL == pFly)
	{
		DEBUGLOG("CS_REQ_OPEN_SLOT : pFly is NULL!");
		return;
	}

	if (pUser->GetVirtualCurrency()->GetZen() < (pFly->_VC_PRICE * -1))
	{
		::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
		pAck->Write2(ACK_NOT_ENOUGH_ZEN);
		pAck->Write1(slot_number);
		pAck->MakePacket(CMD_SC_REQ_OPEN_SLOT);
		pUser->Send(pAck);
		return;
	}

	if (true == NResource::Static::Instance().GetContentsOpenResource()->IsOpen(static_cast<int>(ContentsOpen::CHINA_CONTENTS)))
	{
		NResource::VIPResource::Data & vipData = NResource::Static::Instance().GetVIPResource()->GetData();

		if (vipData._VIP3_VP > pUser->GetUserInfo().reserve4)// && 100 > pUser->GetVirtualCurrency()->GetZen())
		{
			::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
			* pAck	<< static_cast<int16_t>(ACK_NOT_VIP3)
					<< static_cast<uint8_t>(slot_number);
			pAck->MakePacket(CMD_SC_REQ_OPEN_SLOT);
			pUser->Send(pAck);
			return;
		}
	}

	pUser->GetUserInfo().array_Slot[static_cast<int>(slot_number)].open = true;
	pUser->GetUserInfo().array_Slot[static_cast<int>(slot_number)].character_seq = 0;
	pUser->GetUserInfo().array_Slot[static_cast<int>(slot_number)].makeCharacter = false;
	pUser->GetUserInfo().array_Slot[static_cast<int>(slot_number)].remainStatResetCount = 3;

	::Network::Packet * pAck = ::Network::GETPACKETPOOL()->SEND_NEW();
	pAck->Write2(ACK_OK);
	pAck->Write1(slot_number);
	pAck->MakePacket(CMD_SC_REQ_OPEN_SLOT);
	pUser->Send(pAck);

	// VC 처리
	NLogic::NFunction::VCProcess(pUser, EM_VCINDEX_SLOT_OPEN);

	pUser->FlushUserInfo();
}

} /*NCommand*/ } /*ProjectZ*/
